﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Helper
{
    public class Security
    {
        public static byte[] GetHash(string targetString)
        {
            byte[] data = Encoding.Unicode.GetBytes(targetString);
            byte[] result;

            SHA512 shaM = new SHA512Managed();
            result = shaM.ComputeHash(data);
            return result;
        }
        public static string Unprotect(byte[] data)
        {

            try
            {
                return (BytesToString(ProtectedData.Unprotect(data, null, DataProtectionScope.CurrentUser)));
            }
            catch
            {
                return "";
            }
        }

        private static byte[] StringToBytes(string original)
        {
            return System.Text.Encoding.Unicode.GetBytes(original);
        }
        private static string BytesToString(byte[] bytes)
        {
            return System.Text.Encoding.Unicode.GetString(bytes);
        }
        public static byte[] Protect(string data)
        {
            byte[] mydata = StringToBytes(data);
            try
            {
                return ProtectedData.Protect(mydata, null, DataProtectionScope.CurrentUser);
            }
            catch
            {
                return null;
            }
        }
    }
}
